﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class Ctrl : MonoBehaviour
{
    public ClientProtocol _clientProtocol;
    public Text year; //年份Text
    public GameObject group_btn;  //援助表格按钮分组
    public GameObject Prefab_btn;  //表格按钮的预制体
    public List<JsonDataType> list_Yuanzhu = new List<JsonDataType>();   //援助的Json转的list
    public List<YearJsonDataType> list_YearYuanzhu = new List<YearJsonDataType>();   //援助的Json转的list
    public string str_year;//每次按钮点进去的年份
    public int metering = 1;   //上一页下一页计次
    public int sumXuHao; //每次的总序号 用来计算总页数
    int sumPages;  //每次的总页数
    public Text text_currentPage; //当前页
    public Text text_sumPages; //Text总页数组件
    public string str_currentChengShi;//当前是哪个名字的Json文件
    public static Ctrl _ins;

    public Button yearBtn;
    public GameObject group_ShanDong, group_JiangSu, group_ChongQing, group_QiYe;
    //默认状态的texture
    public Sprite[] defaultSprites;
    //选中状态的texture
    public Sprite[] selectSprites;
    String[] chineseNumber = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
    public GameObject panel_SanJi_YuanZhu;
    private void Awake()
    {
        _ins = this;
    }
    void Start()
    {
        string yearjsondata = File.ReadAllText(Path.Combine(Application.streamingAssetsPath, "援助年份" + ".json"));
        list_YearYuanzhu = JsonConvert.DeserializeObject<List<YearJsonDataType>>(yearjsondata);
        foreach (var item in list_YearYuanzhu)
        {
            string newstr = item.省份;
            GameObject newgroup = null;
            Sprite newDefaultSprite = null;
            Sprite newSelectSprite = null;
            switch (newstr)
            {
                case "山东省":
                    newgroup = group_ShanDong;
                    newDefaultSprite = defaultSprites[0];
                    newSelectSprite = selectSprites[0];
                    break;
                case "江苏省":
                    newgroup = group_JiangSu;
                    newDefaultSprite = defaultSprites[1];
                    newSelectSprite = selectSprites[1];
                    break;
                case "重庆市":
                    newgroup = group_ChongQing;
                    newDefaultSprite = defaultSprites[2];
                    newSelectSprite = selectSprites[2];
                    break;
                case "企业":
                    newgroup = group_QiYe;
                    newDefaultSprite = defaultSprites[3];
                    newSelectSprite = selectSprites[3];
                    break;
            }
            foreach (var year in item.年度)
            {

                Button newbtn = Instantiate(yearBtn, newgroup.transform);
                newbtn.name = year.ToString();
                newbtn.GetComponent<Image>().sprite = newDefaultSprite;
                newbtn.GetComponent<SelectedImage>().selectdSprite = newSelectSprite;
                newbtn.GetComponent<HideToggleBackground>().bindObject = panel_SanJi_YuanZhu;
                if (newstr != "重庆市")
                {
                    string ChineseYear = NumberConvertToChinese(year.ToString());
                    newbtn.transform.GetChild(0).GetComponent<Text>().text = ChineseYear;
                    newbtn.onClick.AddListener(delegate ()
                    {
                        SetYear(ChineseYear);
                    });
                }
                else
                {
                    newbtn.transform.GetChild(0).GetComponent<Text>().text = year;
                    newbtn.onClick.AddListener(delegate ()
                    {
                        SetYear(year);
                    });
                }
                
                newbtn.onClick.AddListener(delegate ()
                {
                    newgroup.GetComponent<SelectedGameObject>().setselected(newbtn.GetComponent<SelectedImage>());
                });

                newbtn.onClick.AddListener(delegate ()
                {
                    OnYuanZhuYearBtn(year.ToString());
                });
                newbtn.onClick.AddListener(delegate ()
                {
                    _clientProtocol.OnBtns_PhotoPath(newstr + "/" + year.ToString());
                });
            }
        }
        print(group_ChongQing.GetComponent<RectTransform>().sizeDelta);
    }
    /// <summary>
    /// 数字年份转换成中文年份
    /// </summary>
    /// <param name="year"></param>
    /// <returns></returns>
    public string NumberConvertToChinese(string year)
    {
        string str = null;
        foreach (var c in year)
        {
            str += chineseNumber[(int)c - 48];
        }
        str += "年";
        return str;
    }
    public void SetYear(string str)
    {
        year.text = str;
    }
    /// <summary>
    /// 点击首页的援助按钮加载对应的Json;
    /// </summary>
    /// <param name="str">Json文件名</param>
    public void OnYuanZhuBtn(string str)
    {
        string jsondata = File.ReadAllText(Path.Combine(Application.streamingAssetsPath, str + ".json"));
        list_Yuanzhu = JsonConvert.DeserializeObject<List<JsonDataType>>(jsondata);
        str_currentChengShi = str;
        RRClient.ins.Send(DataTypeEnum.LScreenAndRScreen, OrderTypeEnum.SetPlayMovie, str + "援助.mp4");
    }
    /// <summary>
    /// 援助年份按钮事件
    /// </summary>
    /// <param name="str">年份</param>
    public void OnYuanZhuYearBtn(string str)
    {
        str_year = str;
        for (int i = 0; i < group_btn.transform.childCount; i++)
        {
            Destroy(group_btn.transform.GetChild(i).gameObject);
        }

        for (int i = 0; i < list_Yuanzhu.Count; i++)
        {
            if (list_Yuanzhu[i].年度 == str)
            {
                sumXuHao = list_Yuanzhu[i].序号;
            }
            if (list_Yuanzhu[i].年度 == str && list_Yuanzhu[i].序号 <= 9)
            {

                GameObject obj = Instantiate(Prefab_btn, group_btn.transform);
                obj.transform.GetChild(0).GetComponent<Text>().text = list_Yuanzhu[i].序号.ToString();
                if (list_Yuanzhu[i].项目业主 != null)
                {
                    obj.transform.GetChild(1).GetComponent<Text>().text = list_Yuanzhu[i].项目业主.ToString();
                }
                else
                {
                    obj.transform.GetChild(1).GetComponent<Text>().text = "无";
                }
                if (list_Yuanzhu[i].项目名称 != null)
                {
                    obj.transform.GetChild(2).GetComponent<Text>().text = list_Yuanzhu[i].项目名称.ToString();
                }
                else
                {
                    obj.transform.GetChild(2).GetComponent<Text>().text = "无";
                }
                obj.transform.GetChild(3).GetComponent<Text>().text = list_Yuanzhu[i].ID.ToString();

                if (list_Yuanzhu[i].援助资金 != null)
                {
                    obj.transform.GetChild(4).GetComponent<Text>().text = list_Yuanzhu[i].援助资金.ToString();
                }
            }

        }
        float tempsumPages = sumXuHao / 9.0f;
        sumPages = (int)Math.Ceiling(tempsumPages);
        //print(tempsumPages + "---" + sumPages);
        text_sumPages.text = "/" + sumPages.ToString();


    }
    /// <summary>
    /// 上一页按钮事件
    /// </summary>
    public void OnPreviousPage()
    {
        if (metering > 1)
        {
            metering--;
            text_currentPage.text = metering.ToString();
            for (int i = 0; i < group_btn.transform.childCount; i++)
            {
                Destroy(group_btn.transform.GetChild(i).gameObject);
            }
            for (int i = 0; i < list_Yuanzhu.Count; i++)
            {
                if (list_Yuanzhu[i].年度 == str_year && list_Yuanzhu[i].序号 > 9 * (metering - 1) && list_Yuanzhu[i].序号 <= 9 * metering)
                {
                    GameObject obj = Instantiate(Prefab_btn, group_btn.transform);
                    obj.transform.GetChild(0).GetComponent<Text>().text = list_Yuanzhu[i].序号.ToString();
                    if (list_Yuanzhu[i].项目业主 != null)
                    {
                        obj.transform.GetChild(1).GetComponent<Text>().text = list_Yuanzhu[i].项目业主.ToString();
                    }
                    else
                    {
                        obj.transform.GetChild(1).GetComponent<Text>().text = "无";
                    }
                    if (list_Yuanzhu[i].项目名称 != null)
                    {
                        obj.transform.GetChild(2).GetComponent<Text>().text = list_Yuanzhu[i].项目名称.ToString();
                    }
                    else
                    {
                        obj.transform.GetChild(2).GetComponent<Text>().text = "无";
                    }
                    obj.transform.GetChild(3).GetComponent<Text>().text = list_Yuanzhu[i].ID.ToString();

                    if (list_Yuanzhu[i].援助资金 != null)
                    {
                        obj.transform.GetChild(4).GetComponent<Text>().text = list_Yuanzhu[i].援助资金.ToString();
                    }
                }
            }
        }
    }
    /// <summary>
    /// 下一页按钮事件
    /// </summary>
    public void OnNextPage()
    {
        if (metering < sumPages)
        {
            metering++;
            text_currentPage.text = metering.ToString();
            for (int i = 0; i < group_btn.transform.childCount; i++)
            {
                Destroy(group_btn.transform.GetChild(i).gameObject);
            }
            for (int i = 0; i < list_Yuanzhu.Count; i++)
            {
                if (list_Yuanzhu[i].年度 == str_year && list_Yuanzhu[i].序号 > 9 * (metering - 1) && list_Yuanzhu[i].序号 <= 9 * metering)
                {
                    GameObject obj = Instantiate(Prefab_btn, group_btn.transform);
                    obj.transform.GetChild(0).GetComponent<Text>().text = list_Yuanzhu[i].序号.ToString();
                    if (list_Yuanzhu[i].项目业主 != null)
                    {
                        obj.transform.GetChild(1).GetComponent<Text>().text = list_Yuanzhu[i].项目业主.ToString();
                    }
                    else
                    {
                        obj.transform.GetChild(1).GetComponent<Text>().text = "无";
                    }
                    if (list_Yuanzhu[i].项目名称 != null)
                    {
                        obj.transform.GetChild(2).GetComponent<Text>().text = list_Yuanzhu[i].项目名称.ToString();
                    }
                    else
                    {
                        obj.transform.GetChild(2).GetComponent<Text>().text = "无";
                    }
                    obj.transform.GetChild(3).GetComponent<Text>().text = list_Yuanzhu[i].ID.ToString();

                    if (list_Yuanzhu[i].援助资金 != null)
                    {
                        obj.transform.GetChild(4).GetComponent<Text>().text = list_Yuanzhu[i].援助资金.ToString();
                    }

                }
            }
        }
    }
    /// <summary>
    /// 三级援助的返回按钮事件
    /// </summary>
    public void Onreturn_SanJi()
    {
        metering = 1;
        sumXuHao = 1;   //重置总序号为1个
        text_currentPage.text = metering.ToString();
        //RRClient.ins.Send(DataTypeEnum.LScreenAndRScreen,OrderTypeEnum.Home,"2");
    }

}
public class JsonDataType
{
    public string ID;
    public int 序号;
    public string 年度;
    public string 项目名称;
    public string 项目业主;
    public string 主要建设内容及规模;
    public string 援助资金;
    public string 标题;
}
public partial class YearJsonDataType
{
    public List<string> 年度;
    public string 省份;
}
